﻿package helpers;
/// <summary>
/// A convinience class to test out most of the classical problems arising in chapter 3 of
/// Downey's Little Book of Semaphores.
/// 
/// This class can be used by threads for simple arithmatics
/// </summary>

/**
 *  A convinience class to test out most of the classical problems arising in chapter 3 of
 *  Downey's Little Book of Semaphores.
 */
public class UnsafeCounter
{
    /**
     * A volatile int counter
     */
	private volatile int count;

	/**
	 * Gets an int value representing this counter's current value
	 */
    public int getCount() {
    	return count;
    }
    
    private void setCount(int count) {
    	this.count = count;
    }

    /**
     * Constructs a counter with an initial value
     * @param initialValue the initial value to set this counter to
     */
    public UnsafeCounter(int initialValue)
    {
        setCount(initialValue);
    }

    /**
     * Adds a specific amount to this counter
     * @param amount the amount to add
     */
    public void add(int amount)
    {
        setCount(count + amount);
    }


    /**
     * Subtracts a specific amount from this counter
     * @param amount the amount to subtract
     */
    public void subtract(int amount)
    {
        setCount(count - amount);
    }
}